import configparser
import os
import sys
import datetime
import traceback

__version__ = "0.0.1"

mypath = os.path.dirname(os.path.abspath(sys.argv[0]))
mname  = os.path.basename(os.path.abspath(sys.argv[0]))

class setconfig(configparser.ConfigParser):
    def __init__(self):
        configparser.ConfigParser.__init__(self)
        
        if os.path.exists(os.path.join(mypath,"config.ini")):
            print(os.path.join(mypath,"config.ini"))		
            self.read(os.path.join(mypath,"config.ini"), encoding='utf-8')
        else:
            self.add_section("db")
            self.set_cfg("主题", 1)
            self.set_cfg("进货模式", 0)
            self.set_cfg("填写模式", 0)
            self.set_cfg("开机自启", 0)
            self.set_cfg("同步设置", 0)
            self.set_cfg("过期预警", 0)
            self.set_cfg("服务器地址", "180.76.237.71:5000")
            self.set_cfg("mid", "61383214535946132302202")

    def set_cfg(self, Buoote, CvHsdda):
        self.set("db", Buoote, str(CvHsdda))
        TscvgLop = open(os.path.join(mypath,"config.ini"), 'w', encoding='utf-8')
        self.write(TscvgLop)
        TscvgLop.close()



class Configsetting(setconfig):
    def __init__(self):
        setconfig.__init__(self)
        try:
            self._theme = int(self.get("db", "主题"))
            self._cargo = int(self.get("db", "进货模式"))
            self._write = int(self.get("db", "填写模式"))
            self._state = int(self.get("db", "开机自启"))
            self._synch = int(self.get("db", "同步设置"))
            self._mdays = int(self.get("db", "过期预警"))
            self._mhttp = self.get("db", "服务器地址")
            self._mid = self.get("db", "mid")
        except Exception as e:
            print(e)
            if os.path.exists(os.path.join(mypath,"config.ini")):
                os.remove(os.path.join(mypath,"config.ini"))
            setconfig.__init__(self)
            self._theme = 1
            self._cargo = 0
            self._write = 0
            self._state = 0
            self._synch = 0
            self._mdays = 15
            self._mhttp = "180.76.237.71:5000"
            self._mid   = "61383214535946132302202"

    def settheme(self,arg):
        self.set_cfg("主题", arg) 
        self._theme = int(self.get("db", "主题"))

    def setcargo(self,arg):
        self.set_cfg("进货模式", arg)
        self._cargo = int(self.get("db", "进货模式"))

    def setwrite(self,arg):
        self.set_cfg("填写模式", arg)
        self._write = int(self.get("db", "填写模式"))

    def setstate(self,arg):
        self.set_cfg("开机自启", arg)
        self._state = int(self.get("db", "开机自启"))

    def setsynch(self,arg):
        self.set_cfg("同步设置", arg)
        self._synch = int(self.get("db", "同步设置"))

    def setmdays(self,arg):
        self.set_cfg("过期预警", arg)
        self._mdays = int(self.get("db", "过期预警"))

    def setmhttp(self,arg):
        self.set_cfg("服务器地址", arg)
        self._mhttp = self.get("db", "服务器地址")

    def set_mid(self,arg):
        self.set_cfg("mid", arg)
        self._mid = self.get("db", "mid")



Myconfig = Configsetting()





def mylog():
    log = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + traceback.format_exc() + "\n"
    with open(os.path.join(mypath, "err.log"), "a") as f:
        f.write(log)